package com.yuan.model_app.service.impl;

import com.yuan.model_app.rest.resp.ShowActivityResp;
import com.yuan.model_app.rest.resp.ShowPageDateResp;
import com.yuan.model_app.rest.resp.ShowRepairComplaintResp;
import com.yuan.model_app.service.ManageShowDataService;
import com.yuan.model_community.domain.CommunityBuilding;
import com.yuan.model_community.domain.CommunityHouse;
import com.yuan.model_community.domain.CommunityParking;
import com.yuan.model_community.domain.CommunityUnit;
import com.yuan.model_community.repository.CommunityBuildingDao;
import com.yuan.model_community.repository.CommunityHouseDao;
import com.yuan.model_community.repository.CommunityParkingDao;
import com.yuan.model_community.repository.CommunityUnitDao;
import com.yuan.model_convenient.domain.ConvenientActivity;
import com.yuan.model_convenient.domain.ConvenientComplaint;
import com.yuan.model_convenient.domain.ConvenientRepair;
import com.yuan.model_convenient.repository.ConvenientActivityDao;
import com.yuan.model_convenient.repository.ConvenientComplaintDao;
import com.yuan.model_convenient.repository.ConvenientRepairDao;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ManageShowDataServiceImpl implements ManageShowDataService {

    private final CommunityBuildingDao communityBuildingDao;

    private final CommunityUnitDao communityUnitDao;

    private final CommunityHouseDao communityHouseDao;

    private final CommunityParkingDao communityParkingDao;

    private final ConvenientActivityDao convenientActivityDao;

    private final ConvenientRepairDao convenientRepairDao;

    private final ConvenientComplaintDao convenientComplaintDao;

    @Override
    public ShowPageDateResp showPageDate() {
        ShowPageDateResp showPageDateResp = new ShowPageDateResp();
        List<CommunityBuilding> all = communityBuildingDao.findAll();
        List<CommunityUnit> all1 = communityUnitDao.findAll();
        List<CommunityHouse> all2 = communityHouseDao.findAll();
        List<CommunityParking> all3 = communityParkingDao.findAll();
        showPageDateResp.setBuildingNum(all.size());
        showPageDateResp.setUnitNum(all1.size());
        showPageDateResp.setHouseNum(all2.size());
        showPageDateResp.setParkingNum(all3.size());
        return showPageDateResp;
    }



    @Override
    public List<ShowActivityResp> showActivity() {
        List<ShowActivityResp> showActivityResps = new ArrayList<>();
        List<ConvenientActivity> all = convenientActivityDao.findAll();
        for (ConvenientActivity convenientActivity : all) {
            ShowActivityResp showActivityResp = new ShowActivityResp();
            Integer activityType = convenientActivity.getActivityType();
            showActivityResp.setActivityType(activityType);
            Timestamp createTime = convenientActivity.getCreateTime();
            showActivityResp.setCreateTime(createTime);
            showActivityResps.add(showActivityResp);
        }
        return showActivityResps;
    }

    @Override
    public List<ShowRepairComplaintResp> showPageRepairComplaint() {
        List<ShowRepairComplaintResp> showRepairComplaintResps = new ArrayList<>();
        List<LocalDate> weeklyTimestamps = getWeeklyTimestamps();

        for (LocalDate weeklyDate : weeklyTimestamps) {
            List<ConvenientRepair> convenientRepairDaoAll = convenientRepairDao.findConvenientRepairByTime(weeklyDate);
            List<ConvenientComplaint> complaintDaoAll = convenientComplaintDao.findConvenientComplaintByTime(weeklyDate);

            ShowRepairComplaintResp showRepairResp = new ShowRepairComplaintResp();
            showRepairResp.setCreateTime(weeklyDate);
            showRepairResp.setDataCount(convenientRepairDaoAll.size());

            ShowRepairComplaintResp showComplaintResp = new ShowRepairComplaintResp();
            showComplaintResp.setCreateTime(weeklyDate);
            showComplaintResp.setDataCount(complaintDaoAll.size());

            showRepairComplaintResps.add(showRepairResp);
            showRepairComplaintResps.add(showComplaintResp);
        }

        return showRepairComplaintResps;
    }

    public static List<LocalDate> getWeeklyTimestamps() {
        // 创建一个列表来存储一周内的Timestamp
        List<LocalDate> timestamps = new ArrayList<>();

        // 获取今天的日期
        LocalDate today = LocalDate.now();

        // 遍历一周内的每一天
        for (int i = 0; i < 7; i++) {
            // 获取当天的日期
            LocalDate currentDate = today.minusDays(i);

            // 将Timestamp添加到列表中
            timestamps.add(currentDate);
        }

        // 返回列表（已经是正序，从今天开始到一周前）
        return timestamps;
    }
}
